// pages/knowledge/index/index.js、
const dayjs = require('dayjs')
const {imgUrl} = getApp().globalData

import {knowledgeApi} from '@/utils/request/api/knowledge'


Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageQuery: {
            index: 1,
            pageSize: 20
        },
        imgUrl: imgUrl + 'knowledge/',
        active: 0,
        class1: [
            {
                name: "采购制度",
                value: 0,
            },
            {
                name: "招采流程梳理",
                value: 1,
            },
            {
                name: "投标方案",
                value: 2,
            },
            {
                name: "合同模版",
                value: 3,
            },
            {
                name: "采购制度",
                value: 4,
            }
        ],
        class2: [
            {
                name: "招标采购",
                value: 0,
            },
            {

                name: "投标指南",
                value: 1,
            },
            {
                name: "代理流程",
                value: 2,
            },
            {
                name: "其他",
                value: 3,
            }
        ],
        class1Selsect: 0,
        class2Selsect: 0,
        searchArr: {
            search0: "",
            search1: "",
            search2: ""
        },
        radioList: [
            {
                name: "搜标题",
                value: 0
            },
            {
                name: "搜正文",
                value: 1
            },
            {
                name: "搜上传者",
                value: 2
            }
        ],
        radiosArr: {
            radio0: 0,
            radio1: 0,
            radio2: 0,
        },
        dropValueArr: {
            dropValue1: 0
        },
        dropOptions1: [
            {text: '标签类型', value: 0},
            {text: '新款商品', value: 1},
            {text: '活动商品', value: 2},
        ],
        minDate: new Date(2000, 1, 1).getTime(),
        maxDate: new Date(2099, 12, 31).getTime(),
        pickerTime: dayjs().unix(),
        uploadTime: "",
        uploadTimeShow: false,
        dataDownloadList: [],
        videoList: [{}, {}, {}],
        selectsShow: false,
        schemeShow:false,
        information: [{}, {}],
        schemeColumns:['跨境电商', '汽车', '房地产', '煤炭']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    async onShow() {
        this.queryPage()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    onChange(evn) {
        this.setData({
            active: evn.detail.index,
            'pageQuery.index': 1,
            'pageQuery.pageSize': 20
        })
        this.queryPage(evn.detail.index)
    },
    schemeClose(){
        this.setData({
            schemeShow:false
        })
    },
    schemeChange(evn){

    },
    async queryPage(type = 0) {
        if (type == 0) {
            //资料下载
            let params = {
                page: this.data.pageQuery.index,
                page_size: this.data.pageQuery.pageSize,
                keyword: this.data.searchArr.search0,
                author_type:this.data.radiosArr.radio0,
                tags:''
            }

            let res = await knowledgeApi.documentUrl('list', params)
            if (res.data.list.length > 0) {
                this.setData({
                    dataDownloadList: res.data.list.map(item=>{
                        item.created_at = dayjs(item.created_at).format('YYYY-MM-DD HH:DD')
                        item.updated_at = dayjs(item.updated_at).format('YYYY-MM-DD HH:DD')
                        return item
                    })
                })
            }
        } else if (type == 1) {
            //知库学堂
            let params = {
                page: this.data.pageQuery.index,
                page_size: this.data.pageQuery.pageSize,
                category: ''
            }

            let res = await knowledgeApi.courseUrl('list', params)
            console.log(`res.data.list`, res.data)
            if (res.data.courses.length > 0) {
                this.setData({
                    videoList: res.data.courses
                })
            }
        } else if (type == 2) {
            //政策咨讯
            let params = {
                page: this.data.pageQuery.index,
                page_size: this.data.pageQuery.pageSize,
                keyword: ''
            }

            let res = await knowledgeApi.lawPolicyUrl('list', params)
            if (res.data.list.length > 0) {
                this.setData({
                    information: res.data.list
                })
            }
        }

    },

    async downInformation(evn){
        //下载
        let val = evn.currentTarget.dataset.items
        let res = await knowledgeApi.postDocumentUrl('download', {
            document_id:val.id
        })
        if(res.err_code == 0){

        }else{
            wx.showToast({
                title: res.err_msg,  // 标题
                duration: 1500   // 图标停留时间，默认1500ms
            })
        }
    },
    async collect(evn){
        //收藏
        let val = evn.currentTarget.dataset.items
        let res = await knowledgeApi.postDocumentUrl('collect', {
            document_id:val.id
        })
        if(res.err_code == 0){
            wx.showToast({
                title: '收藏成功！',  // 标题
                icon: 'success',   // 图标类型，默认success 图标支持开发文档的icon
                duration: 1500   // 图标停留时间，默认1500ms
            })
        }else{
            wx.showToast({
                title: res.err_msg,  // 标题
                duration: 1500   // 图标停留时间，默认1500ms
            })
        }
    },
    onClass(evn) {
        let type = evn.currentTarget.dataset.classtype
        if (type == 0) {
            this.setData({
                class1Selsect: evn.currentTarget.dataset.values
            })
        } else if (type == 1) {
            this.setData({
                class2Selsect: evn.currentTarget.dataset.values
            })
        }

    },
    bindScheme(){
        this.setData({
            schemeShow: true
        })
    },
    bindClass() {
        this.setData({
            selectsShow: true
        })
        let filedir = this.selectComponent('#selectsPop')
        filedir.initCandidateFun(0)
    },
    bindTimes() {
        this.setData({
            uploadTimeShow: true,
        })
    },
    onInput(event) {
        this.setData({
            pickerTime: event.detail,
            uploadTime: dayjs(event.detail).format('YYYY-MM-DD'),
            uploadTimeShow: false
        });
    },
    closePiceker() {
        this.setData({
            uploadTimeShow: false
        });
    },
    radiosCheckFun(evn) {
        this.radiosArr = {
            radio0: evn.currentTarget.dataset.values,
            radio1: 0,
            radio2: 0,
        }
        this.setData({
            radiosArr: this.radiosArr
        })
    },
    goDetailsPage(evn) {
        wx.navigateTo(
            {
                url: `/pages/knowledge/documentDetail/documentDetail?id=${evn.currentTarget.dataset.items.id}`
            }
        )
    },
    getSelectsPop(evn) {
        this.setData({
            selectsShow: true
        })
        let filedir = this.selectComponent('#selectsPop')
        filedir.initCandidateFun(evn.currentTarget.dataset.selects)
    },
    goVideoPage(evn) {
        console.log(`evn.currentTarget.dataset`,evn.currentTarget.dataset)
        // return
        wx.navigateTo(
            {
                url: `/pages/knowledge/videoPage/videoPage?ids=${evn.currentTarget.dataset.items.id}`
            }
        )
    },
    goInformation(evn) {
        wx.navigateTo(
            {
                url: `/pages/knowledge/politicsLaw/politicsLaw?id=${evn.currentTarget.dataset.items.id}`
            }
        )
    },
    tapConfirm(val) {
        this.setData({
            selectsShow: false
        })
    }
})